package com.example.test_witns.adapter.images;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.example.test_witns.R;

import java.util.List;

public class ImagesAdapter extends RecyclerView.Adapter<ImagesAdapter.ViewHolder>{

    private List<String> imageUrls;
    private OnHeightChangeListener heightChangeListener;
    private RecyclerView recyclerView;

    public ImagesAdapter(List<String> imageUrls, RecyclerView recyclerView, OnHeightChangeListener heightChangeListener) {
        this.imageUrls = imageUrls;
        this.recyclerView = recyclerView;
        this.heightChangeListener = heightChangeListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        String imageUrl = imageUrls.get(position);
        Glide.with(holder.itemView.getContext())
                .asBitmap()
                .load(imageUrl)
                .listener(new RequestListener<Bitmap>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                        if (position == imageUrls.size() - 1) { // 最后一张图片加载完成
                            int totalHeight = 0;
                            for (int i = 0; i < recyclerView.getChildCount(); i++) {
                                totalHeight += recyclerView.getChildAt(i).getHeight();
                            }
                            heightChangeListener.onHeightChange(totalHeight);
                        }
                        return false;
                    }
                })
                .into(holder.imageView);
    }

    @Override
    public int getItemCount() {
        return imageUrls.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
        }
    }

    public void addImage(String imageURL) {
        imageUrls.add(imageURL);
        notifyItemInserted(imageUrls.size() - 1);
    }

    public interface OnHeightChangeListener {
        void onHeightChange(int height);
    }
}
